﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace WebApplication1
{
    public partial class KometariAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                LoadProizvoditeli();
            }
        }

        protected void LoadProizvoditeli()
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT ImeProizvoditel FROM proizvoditeli";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    ListItem element = new ListItem();
                    element.Text = citac["ImeProizvoditel"].ToString();
                    element.Value = citac["ImeProizvoditel"].ToString();
                    ProizvoditeliListBox.Items.Add(element);
                }
                citac.Close();
            }
            catch (Exception ex)
            {
                // Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void LoadModeli(String Marka)
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT Model FROM modeli WHERE Proizvoditel='" + Marka + "'";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    ListItem element = new ListItem();
                    element.Text = citac["Model"].ToString();
                    element.Value = citac["Model"].ToString();
                    ModeliListBox.Items.Add(element);
                    txtKomentar.ToolTip = "Коментар за моделот " + ProizvoditeliListBox.SelectedValue + " " + ModeliListBox.SelectedValue;
                }
                citac.Close();
            }
            catch (Exception ex)
            {
                //   Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void LoadKomentari(String Model)
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT Broj FROM komentari WHERE Model='" + Model + "' AND Odobreno=0";

            try
            {
                konekcija.Open();

                int br = 1;
                MySqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    ListItem element = new ListItem();
                    element.Text = "Коментар " + (br++).ToString();
                    element.Value = citac["Broj"].ToString();
                    KomentariListBox.Items.Add(element);
                }
                citac.Close();
            }
            catch (Exception ex)
            {
                lblTest.Text = ex.Message;
                //   Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void LoadKomentar(String BrojKomentar)
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT * FROM komentari WHERE Broj='" + BrojKomentar + "'";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    txtKomentar.Text = citac["Komentar"].ToString();
                    lblOdKogo.Text = "Објавено од " + citac["OdKogo"].ToString();
                }
                citac.Close();
            }
            catch (Exception ex)
            {
                //   Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void ProizvoditeliListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModeliListBox.Items.Clear();
            KomentariListBox.Items.Clear();
            txtKomentar.Text = "";
            lblOdKogo.Text = " ";
            LoadModeli(ProizvoditeliListBox.SelectedValue);
        }

        protected void ModeliListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            KomentariListBox.Items.Clear();
            txtKomentar.Text = "";
            lblOdKogo.Text = " ";
            LoadKomentari(ModeliListBox.SelectedValue);
        }

        protected void KomentariListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadKomentar(KomentariListBox.SelectedValue);
        }
    }
}